use utf8;

############################################################################################
#                                Définition de l'onglet Jeu                                #
############################################################################################

my $games = $blocnote->add('Jeux', -label => 'Jeux',);
$games->configure( -background => 'white',);

# Message de retour
my $messageGame = '';

#Mise en place du titre
my $c = $games->Canvas(-width => 500, -height => 1, -background => 'white', -borderwidth => 0)->pack(-pady => 2);
$c->createLine(1,1, 500,1); 
$games->Label ( -text => "Rechercher / Ajouter un Jeu", -background => 'white')->pack();
$c = $games->Canvas(-width => 500, -height => 1, -background => 'white', -borderwidth => 0)->pack();
$c->createLine(1,1, 500,1); 

#Définition des champs de recherche
my $fieldGames = $games->Frame(-background => 'white')->pack(-anchor => 'n', -pady => 15, -padx => 15);

$fieldGames->Label( -text => 'Nom:', -background => 'white', -justify => 'left')->grid(-row => 0, -column => 0, -sticky => 'w');
my $name = $fieldGames->Entry()->grid(-row => 0, -column => 1, -sticky => 'w');

$fieldGames->Label( -text => 'Etat:', -background => 'white',)->grid(-row => 1, -column => 0, -sticky => 'w',);
my $condition = '';
$fieldGames->BrowseEntry(-listheight => 4, -choices => ['Mauvais', 'Moyen', 'Bon', 'Très Bon'], -variable => \$condition)->grid(-row => 1, -column => 1, -sticky => 'w');

$fieldGames->Label( -text => "Date d'achat:", -background => 'white', -justify => 'left')->grid(-row => 2, -column => 0, -sticky => 'w');
my $date = $fieldGames->Entry()->grid(-row => 2, -column => 1, -sticky => 'w');

$fieldGames->Label( -text => "Prix d'achat:", -background => 'white', -justify => 'left')->grid(-row => 3, -column => 0, -sticky => 'w');
my $price = $fieldGames->Entry()->grid(-row => 3, -column => 1, -sticky => 'w');

$fieldGames->Label( -text => "Nombre min de joueurs:", -background => 'white', -justify => 'left')->grid(-row => 4, -column => 0, -sticky => 'w');
my $nbMinPlayer = $fieldGames->Entry(-width => 2)->grid(-row => 4, -column => 1, -sticky => 'w');

$fieldGames->Label( -text => "Nombre max de joueurs:", -background => 'white', -justify => 'left')->grid(-row => 5, -column => 0, -sticky => 'w');
my $nbMaxPlayer = $fieldGames->Entry(-width => 2)->grid(-row => 5, -column => 1, -sticky => 'w');

my $buttonFrame = $games->Frame(-background => 'white')->pack(-anchor => 'n');

$buttonFrame->Button(-text => 'Ajouter',
                     -command => [sub{
				            if($name->get ne '' and 
				               $date->get ne '' and 
				               $price->get ne '' and 
				               $condition ne '' and 
				               $nbMaxPlayer->get ne '' and 
				               $nbMinPlayer->get ne ''){
				                AddGame();
                    }
                    else{
                    	$messageGame = "Pour ajouter un jeu merci de renseigner tous les champs.";
                    }}])->grid(-row => 6, -column => 0, -pady => 5, -sticky => 'w', -padx => 7);
$buttonFrame->Button(-text => 'Rechercher', -command => [\&SearchGame])->grid(-row => 6, -column => 1, -pady => 5, -sticky => 'w', -padx => 7);
$buttonFrame->Button(-text => 'Effacer', -command =>[\&ClearGame,$name,$date,$price,$nbMaxPlayer,$nbMinPlayer, \$condition])->grid(-row => 6, -column => 2, -pady => 5, -sticky => 'w', -padx => 7);

$games->Label(-textvariable => \$messageGame, -background => 'white')->pack();

############################################################################################
#                               Fonctions de gestion des Jeux                              #
############################################################################################

# Fonction ajoutant un jeu
sub AddGame{
	my $dialog_box = $window->DialogBox(-title => 'Ajout d\'un Jeu', -buttons => ["OK"], -background => 'white');
  	gestion_bd::addGame($name->get,$date->get,$price->get,$nbMinPlayer->get,$nbMaxPlayer->get,$condition);
  	$dialog_box->Label(-text => "Jeu ajouté !", -background => 'white')->pack();
  	$dialog_box->Show();
}

# Fonction supprimant un jeu
sub DeleteGame {
  my $dialog_box = $window->DialogBox(-title  => 'Suppression d\'un jeu', -buttons => ["OK"], -background => 'white');
  gestion_bd::deleteGame($_[0]);
  $dialog_box->Label(-text => "Jeu supprimé !", -background => 'white')->pack();
  $dialog_box->Show();
}

# Fonction affichant le résultat de la recherche d'un jeu
sub SearchGame{
  my $i = 1;
  my $dialog_box = $window->DialogBox(-title => 'Résultat de la recherche', -buttons => ["Noter", "Afficher les notes", "Modifier", "Supprimer", "Quitter"], -background => 'white');

  $dialog_box->Label(-text => "Nom", -relief =>'groove',)->grid(-row => 0, -column => 1, -sticky => "nsew");
  $dialog_box->Label(-text => "Nombre de joueurs", -relief =>'groove',)->grid(-row => 0, -column => 2, -sticky => "nsew");
  $dialog_box->Label(-text => "Date d'achat", -relief =>'groove',)->grid(-row => 0, -column => 3, -sticky => "nsew");
  $dialog_box->Label(-text => "Prix d'achat", -relief =>'groove',)->grid(-row => 0, -column => 4, -sticky => "nsew");
  $dialog_box->Label(-text => "Etat", -relief =>'groove',)->grid(-row => 0, -column => 5, -sticky => "nsew");

  my $gamesList = gestion_bd::searchGame($name->get,$date->get,$price->get,$nbMaxPlayer->get,$nbMinPlayer->get,$condition);
  my $game;

  foreach my $g (sort {$gamesList->{$a}{nomJeu} cmp $gamesList->{$b}{nomJeu}} keys %{$gamesList}) {
    $dialog_box->Radiobutton(-value=>$gamesList->{$g}{nomJeu}, -variable=>\$game)->grid(-row => $i, -column => 0,);
    $dialog_box->Label(-text => $gamesList->{$g}{nomJeu}, -background => 'white')->grid(-row => $i, -column => 1,);
    $dialog_box->Label(-text => "$gamesList->{$g}{joueursMin}-$gamesList->{$g}{joueursMax}", -background => 'white')->grid(-row => $i, -column => 2,);
    $dialog_box->Label(-text => gestion_bd::convertDate($gamesList->{$g}{dateAchat}), -background => 'white')->grid(-row => $i, -column => 3,);
    $dialog_box->Label(-text => $gamesList->{$g}{prixAchat}.'€', -background => 'white')->grid(-row => $i, -column => 4,);
    $dialog_box->Label(-text => $gamesList->{$g}{etat}, -background => 'white')->grid(-row => $i, -column => 5,);
    $i = $i + 1;
  }

  my $reponse = $dialog_box->Show();

  if ($reponse eq "Modifier" and defined($game)) {
    ModifyGame($game);
  }
  elsif ($reponse eq "Supprimer" and defined($game)) {
    DeleteGame($game);
  }
  elsif ($reponse eq "Noter" and defined($game)) {
    MarkGame($game);
  }
  elsif ($reponse eq "Afficher les notes" and defined($game)) {
    MarkOfGame($game);
  }
  elsif ($reponse eq "Modifier" or $reponse eq "Supprimer" or $reponse eq "Noter") {
    errorBox("Veuillez selectionner un jeu !");
    SearchGame();
  }
}

# Fonction affichant les notes pour un jeu donné
sub MarkOfGame{
  my $i = 1;
  my $dialog_box = $window->DialogBox(-title => "Notes du jeu $_[0]", -buttons => ["Quitter"], -background => 'white');

  $dialog_box->Label(-text => "Login", -relief =>'groove',)->grid(-row => 0, -column => 1, -sticky => "nsew");
  $dialog_box->Label(-text => "Note", -relief =>'groove',)->grid(-row => 0, -column => 2, -sticky => "nsew");
  $dialog_box->Label(-text => "Commentaire", -relief =>'groove',)->grid(-row => 0, -column => 3, -sticky => "nsew");

  my $commitList = gestion_bd::getMarkOfGame($_[0]);

  foreach my $c (sort {$commitList->{$a}{login} cmp $commitList->{$b}{login}} keys %{$commitList}) {
    $dialog_box->Label(-text => $commitList->{$c}{login}, -background => 'white')->grid(-row => $i, -column => 1);
    $dialog_box->Label(-text => $commitList->{$c}{note}."/10", -background => 'white')->grid(-row => $i, -column => 2);
    $dialog_box->Label(-text => $commitList->{$c}{commentaire}, -background => 'white')->grid(-row => $i, -column => 3);
    $i = $i + 1;
  }

  $dialog_box->Show();
}

# Fonction permettant de modifier les informations d'un jeu
sub ModifyGame {
	my $dialog_box = $window->DialogBox(-title => "Modification du jeu $_[0]", -buttons    => ["Enregistrer", "Annuler"], -background => 'white', -default_button => 'Annuler');
	 
	my $gamesList = gestion_bd::searchGame($_[0], "", "", "", "", "");
	my $fieldGames = $dialog_box->Frame(-background => 'white')->pack(-anchor => 'n', -pady => 15, -padx => 15);

	$fieldGames->Label( -text => 'Etat:', -background => 'white',)->grid(-row => 0, -column => 0, -sticky => 'w',);
	my $condition = $gamesList->{$_[0]}{etat};
	$fieldGames->BrowseEntry(-listheight => 4, -choices => ['Mauvais', 'Moyen', 'Bon', 'Très Bon'], -variable => \$condition)->grid(-row => 0, -column => 1, -sticky => 'w');

	$fieldGames->Label( -text => "Date d'achat:", -background => 'white', -justify => 'left')->grid(-row => 1, -column => 0, -sticky => 'w');
	my $date = $fieldGames->Entry(-textvariable => gestion_bd::convertDate($gamesList->{$_[0]}{dateAchat}))->grid(-row => 1, -column => 1, -sticky => 'w');

	$fieldGames->Label( -text => "Prix d'achat:", -background => 'white', -justify => 'left')->grid(-row => 2, -column => 0, -sticky => 'w');
	my $price = $fieldGames->Entry(-textvariable => $gamesList->{$_[0]}{prixAchat})->grid(-row => 2, -column => 1, -sticky => 'w');

	$fieldGames->Label( -text => "Nombre min de joueurs:", -background => 'white', -justify => 'left')->grid(-row => 3, -column => 0, -sticky => 'w');
	my $nbMinPlayer = $fieldGames->Entry(-width => 2,-textvariable => $gamesList->{$_[0]}{joueursMin})->grid(-row => 3, -column => 1, -sticky => 'w');

	$fieldGames->Label( -text => "Nombre max de joueurs:", -background => 'white', -justify => 'left')->grid(-row => 4, -column => 0, -sticky => 'w');
	my $nbMaxPlayer = $fieldGames->Entry(-width => 2,-textvariable => $gamesList->{$_[0]}{joueursMax})->grid(-row => 4, -column => 1, -sticky => 'w');

  my $reponse = $dialog_box->Show();

  if ($reponse eq "Enregistrer") {
  	gestion_bd::updateGame($_[0],$date->get,$price->get,$nbMaxPlayer->get,$nbMinPlayer->get,$condition);
    dialogBox("Information","Modification enregistrée !");
	}
}

# Fonction permettant de noter un jeu
sub MarkGame {
	my $dialog_box = $window->DialogBox(-title => "Notation du jeu $_[0]", -buttons    => ["Enregistrer", "Annuler"], -background => 'white', -default_button => 'Annuler');
	 
	my $fieldGames = $dialog_box->Frame(-background => 'white')->pack(-anchor => 'n', -pady => 15, -padx => 15);

	$fieldGames->Label(-text => "Login:", -background => 'white', -justify => 'left')->grid(-row => 0, -column => 0, -sticky => 'w');
	my $login = $fieldGames->Entry()->grid(-row => 0, -column => 1,-columnspan => 3, -sticky => 'w');

	$fieldGames->Label(-text => "Note:", -background => 'white', -justify => 'left')->grid(-row => 1, -column => 0, -sticky => 'w');
	my $mark = $fieldGames->Entry(-width => 2)->grid(-row => 1, -column => 1, -sticky => 'w');
	$fieldGames->Label(-text => "/10", -background => 'white', -justify => 'left')->grid(-row => 1, -column => 2, -sticky => 'w');
	$fieldGames->Label(-text => "                                                    ", -background => 'white', -justify => 'left')->grid(-row => 1, -column => 3, -sticky => 'w');

	$fieldGames->Label(-text => "Commentaire:", -background => 'white', -justify => 'left')->grid(-row => 2, -column => 0, -sticky => 'nw');
	my $com = $fieldGames->Text(-width => 40, -height => 10)->grid(-row => 2, -column => 1,-columnspan => 3);

	my $reponse = $dialog_box->Show();

  if ($reponse eq "Enregistrer" and $login->get ne '' and $mark->get ne '' and $mark->get <= 10  and $com->get('1.0', 'end') ne '') {
  	my $id = gestion_bd::getIdSubscriber($login->get);

  	if(defined($id)){
  		gestion_bd::markGame($_[0], $id, $com->get('1.0', 'end'), $mark->get);
      dialogBox("Information","Note enregistrée !");
  	}
  	else{
  		errorBox("Le login ne correspond à aucun adhérent connu !");
  		MarkGame($_[0]);
  	}
	}
	elsif($reponse eq "Enregistrer" and ($login->get eq '' or $mark->get eq '' or $com->get('1.0', 'end') eq '')){
		errorBox("Merci de remplir tous les champs !");
		MarkGame($_[0]);
	}
  elsif($reponse eq "Enregistrer" and $mark->get <= 10){
    errorBox("Merci de mettre une note inférieure ou égale à dix !");
  }
}





